package com.bigdata.uno.repository.base;


import com.bigdata.uno.common.model.catalogue.CataloguePoJo;
import com.bigdata.uno.common.model.information.InfoPoJo;
import com.bigdata.uno.common.model.requirement.RequirementPoJo;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * Repository 代码生成器
 *
 * @author zaoshu
 */
public class RepositoryGenerator {
    private static final String REPOSITORY_PACKAGE = "com.bigdata.uno.repository";
    private static final String CODE_TEMPLATE = "package {{repository-package}};\n"
            + "\n"
            + "import java.util.List;\n"
            + "\n"
            + "import org.apache.ibatis.annotations.InsertProvider;\n"
            + "import org.apache.ibatis.annotations.Options;\n"
            + "import org.apache.ibatis.annotations.Param;\n"
            + "import org.apache.ibatis.annotations.SelectProvider;\n"
            + "import org.apache.ibatis.annotations.UpdateProvider;\n"
            + "import org.jooq.QueryPart;\n"
            + "import org.springframework.stereotype.Repository;\n"
            + "\n"
            + "import com.github.pagehelper.Page;\n"
            + "import {{entity-class-full-name}};\n"
            + "import com.bigdata.uno.repository.base.ListOption;\n"
            + "import com.bigdata.uno.repository.base.AbstractSqlProvider;\n"
            + "import com.bigdata.uno.repository.base.BaseRepository;\n"
            + "import com.bigdata.uno.repository.base.RepositoryGenerator;\n"
            + "\n"
            + "/**\n"
            + " * {{entity-class-name}}Repository generated by {@link RepositoryGenerator}\n"
            + " *\n"
            + " * @author {{author}}\n"
            + " */\n"
            + "@Repository\n"
            + "public interface {{entity-class-name}}Repository extends BaseRepository<{{entity-class-name}}> {\n"
            + "\n"
            + "    class SqlProvider extends AbstractSqlProvider {\n"
            + "        @Override\n"
            + "        public String table() {\n"
            + "            return \"{{entity-table}}\";\n"
            + "        }\n"
            + "    }\n"
            + "\n"
            + "    @Override\n"
            + "    @SelectProvider(type = SqlProvider.class, method = \"selectById\")\n"
            + "    {{entity-class-name}} selectById(Long id);\n"
            + "\n"
            + "    @Override\n"
            + "    @SelectProvider(type = SqlProvider.class, method = \"selectOne\")\n"
            + "    {{entity-class-name}} selectOne(QueryPart where);\n"
            + "\n"
            + "    @Override\n"
            + "    @SelectProvider(type = SqlProvider.class, method = \"selectWhere\")\n"
            + "    List<{{entity-class-name}}> selectWhere(QueryPart where);\n"
            + "\n"
            + "    @Override\n"
            + "    @SelectProvider(type = SqlProvider.class, method = \"selectAll\")\n"
            + "    List<{{entity-class-name}}> selectAll();\n"
            + "\n"
            + "    @Override\n"
            + "    @InsertProvider(type = SqlProvider.class, method = \"insert\")\n"
            + "    @Options(useGeneratedKeys = true)\n"
            + "    int insert({{entity-class-name}} entity);\n"
            + "\n"
            + "    @Override\n"
            + "    @UpdateProvider(type = SqlProvider.class, method = \"updateByFields\")\n"
            + "    int updateByFields(@Param(\"old\") {{entity-class-name}} oldEntity,\n"
            + "                       @Param(\"new\") {{entity-class-name}} newEntity);\n"
            + "\n"
            + "    @Override\n"
            + "    @UpdateProvider(type = SqlProvider.class, method = \"updateNotNullFields\")\n"
            + "    int updateNotNullFields({{entity-class-name}} entity);\n"
            + "\n"
            + "    @Override\n"
            + "    @UpdateProvider(type = SqlProvider.class, method = \"softDelete\")\n"
            + "    int delete(Long id);\n"
            + "\n"
            + "    @Override\n"
            + "    @UpdateProvider(type = SqlProvider.class, method = \"softDeleteWhere\")\n"
            + "    int deleteWhere(QueryPart where);\n"
            + "\n"
            + "    @Override\n"
            + "    @SelectProvider(type = SqlProvider.class, method = \"selectPage\")\n"
            + "    Page<{{entity-class-name}}> selectPage(@Param(\"option\") ListOption option,\n"
            + "                               @Param(\"pageNumKey\") Long pageNum,\n"
            + "                               @Param(\"pageSizeKey\") Long pageSize);\n"
            + "\n"
            + "}\n";

    private static void generate(String author, String table, Class<?> entityClass) {
        String code = CODE_TEMPLATE
                .replaceAll("\\{\\{author}}", author)
                .replaceAll("\\{\\{entity-table}}", table)
                .replaceAll("\\{\\{entity-class-full-name}}", entityClass.getName())
                .replaceAll("\\{\\{entity-class-name}}", entityClass.getSimpleName())
                .replaceAll("\\{\\{repository-package}}", REPOSITORY_PACKAGE);
        System.out.println(code);

        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(code);
        clip.setContents(tText, null);

        System.out.println("\n\nPaste code on the package: " + REPOSITORY_PACKAGE);
    }

    public static void main(String[] args) {
        generate("bchen", "catalogue", CataloguePoJo.class);
    }
}
